package com.gc.controller;

import com.gc.common.resp.ApiResult;
import com.gc.pojo.SysDevice;
import com.gc.pojo.SysDeviceRecord;
import com.gc.pojo.SysUser;
import com.gc.services.DeviceRecordService;
import com.gc.services.DeviceService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
    @RequestMapping("device-record")
public class DeviceRecordController {

    @Autowired
    private DeviceRecordService deviceRecordService;

    @Autowired
    private DeviceService deviceService;

    @GetMapping("record-submit-view")
    public ModelAndView toDeviceView(){
        ModelAndView mav = new ModelAndView("add");
        List<SysDevice> devices =  deviceService.getAllDevice();
        mav.addObject("devices",devices);
        return mav;
    }

    @ResponseBody
    @PostMapping("submit")
    public ApiResult submitDeviceRecord(@RequestBody SysDeviceRecord record){
        ApiResult result = new ApiResult();
        Subject subject = SecurityUtils.getSubject();
        SysUser currentUser = (SysUser) subject.getPrincipal();
        record.setApplyBy(currentUser.getUserId());
        record.setStatus(0);  //设置状态  0为申请中
        Integer count = deviceRecordService.addDeviceRecord(record);
        if(count != 1){
            return new ApiResult(500,"上传失败!");
        }
        return result;
    }

    @ResponseBody
    @GetMapping("getMyDeviceRecord")
    public List<SysDeviceRecord> getMyDeviceRecord(){
        Subject subject = SecurityUtils.getSubject();
        SysUser currentUser = (SysUser) subject.getPrincipal();
        List<SysDeviceRecord> list = deviceRecordService.getMyDeviceRecord(currentUser.getUserId());
        return list;
    }
}